package com.bobo.serviceorder.component;

import com.bobo.serviceorder.dao.TbEventServiceOrderDao;
import com.bobo.serviceorder.entry.TbEventServiceOrder;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author bobo
 * @date 2021-04-03
 */
@Component
public class ConsumerQueue {

    @Autowired
    private TbEventServiceOrderDao tbEventServiceOrderDao;

    @JmsListener(destination = "ActiveMQQueue",containerFactory = "jmsListenerContainerFactory")
    public void receive(TextMessage textMessage, Session session) throws JMSException {
        try {
            System.out.println("收到的消息："+textMessage.getText());
            String content = textMessage.getText();

            TbEventServiceOrder servicePay = (TbEventServiceOrder) JSONObject.toBean(JSONObject.fromObject(content),TbEventServiceOrder.class);
            System.out.println(servicePay);
            tbEventServiceOrderDao.insert(servicePay);
            // 业务完成，确认消息 消费成功
            textMessage.acknowledge();
        }catch (Exception e){
            // 回滚消息
            e.printStackTrace();
            System.out.println("异常了");
            session.recover();
        }

    }

    /**
     * 补偿 处理（人工，脚本）。自己根据自己情况。
     * @param text
     */
    @JmsListener(destination = "DLQ.ActiveMQQueue")
    public void receive2(String text){
        System.out.println("死信队列:"+text);
    }

}
